package com.action.app_chat;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;


import com.enetiy.Useraccount;
import com.enetiy.Userinfo;
import com.enetiy.Users;
import com.operation.ChatRongyun;
import com.service.LoginService;
import com.service.UseraccountService;
import com.service.UserinfoService;
import com.util.DataUtil;
import com.util.IPUtil;

public class UsersInfo {
	private LoginService loginService;
	private UseraccountService useraccountService;
	private UserinfoService userinfoService;
	private String[] userids;//安卓传值
	private String jsonuserid;//iOS传值
	private String phone;
	private int userid;
	/**
	 *用户信息   >Nf053
	 */
	public void getUserinfo(){
		//iOS的Json数组解析注入userids数组
		if(userids==null){
			System.out.println(jsonuserid);
			JSONArray jsonArray = new JSONArray(jsonuserid);
			int iSize = jsonArray.length();
			System.out.println("Size:" + iSize);
			userids =  new String [iSize];
			for (int i = 0; i < iSize; i++) {
				org.json.JSONObject jsonObj = jsonArray.getJSONObject(i);
				System.out.println(jsonObj.getString("userids"));
				userids[i]=jsonObj.getString("userids");
			}
		}
		
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		JSONObject sendJson = DataUtil.sendJson();
		try {
			out = response.getWriter();
			JSONArray jsonList =new JSONArray();
			System.out.println(userids);
			List<Users> userslist = loginService.selectUserList(userids);
			if(!userslist.isEmpty()){
				for (int i = 0; i < userslist.size(); i++){
					JSONObject json =new JSONObject();
					json.put("userid", userslist.get(i).getId());
					json.put("nickname", userslist.get(i).getNickname());
					json.put("headurl",IPUtil.getIp()+"/img/uersImg/headImg/"+userslist.get(i).getHeadImg());
					jsonList.put(json);
				}
			}else{
				sendJson.put("code", -1);
				sendJson.put("msg","没数据");
			}
			sendJson.put("code", 1);
			sendJson.put("userinfo", jsonList.toString());
			out.print(sendJson);
		} catch (Exception e) {
			sendJson.put("code", -1);
			out.print(sendJson);
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 搜索用户    >Nf054
	 */
	public void seekUserinfo(){
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		JSONObject sendJson = DataUtil.sendJson();
		try {
			out = response.getWriter();
			Useraccount users = useraccountService.selectUseraccount(phone);
			if(users!=null){
				sendJson.put("code", 1);
				sendJson.put("userid", users.getUsers().getId());
				sendJson.put("nickname", users.getUsers().getNickname());
				sendJson.put("headurl", IPUtil.getIp()+"/img/uersImg/headImg/"+users.getUsers().getHeadImg());
			}else{
				sendJson.put("code", 0);
			}
			out.print(sendJson);
		} catch (Exception e) {
			sendJson.put("code", -1);
			out.print(sendJson);
			e.printStackTrace();
		}
	}
	/**
	 * 融云token注册
	 */
	public void enrollChat(){
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		JSONObject sendJson = DataUtil.sendJson();
		try {
			out = response.getWriter();
			Userinfo userinfo = userinfoService.selectUserinfo(userid);
			if(userinfo!=null){
				String chatToken = ChatRongyun.getChatToken(userid,userinfo.getUsers().getNickname(),userinfo.getUsers().getHeadImg());
				userinfo.setChatToken(chatToken);
				userinfoService.updateUserinfo(userinfo);
				sendJson.put("code", 1);
				sendJson.put("chatToken", chatToken);
				sendJson.put("headurl",IPUtil.getIp()+"/img/uersImg/headImg/"+userinfo.getUsers().getHeadImg());
				sendJson.put("nickname", userinfo.getUsers().getNickname());
			}else{
				sendJson.put("code", 0);
				sendJson.put("msg", "用户不存在");
			}
		} catch (Exception e) {
			sendJson.put("code", -1);
			e.printStackTrace();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
		}
		out.print(sendJson);
	}
	

	public void setJsonuserid(String jsonuserid) {
		this.jsonuserid = jsonuserid;
	}

	public void setUserids(String[] userids) {
		this.userids = userids;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	public void setUseraccountService(UseraccountService useraccountService) {
		this.useraccountService = useraccountService;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setUserinfoService(UserinfoService userinfoService) {
		this.userinfoService = userinfoService;
	}
	
}
